<?php
// $Id: search.inc.php 236 2008-03-03 13:38:35Z mikhail.miguel $

// 2007-07-14 K.OHWADA
// urlencode keyword

// 2007-03-01 K.OHWADA
// hack for multi site

// 2006-09-20 K.OHWADA
// show context for suin's search
// highlight_keyword

// 2006-05-15 K.OHWADA
// change $weblinks_dirname to capital letter

// 2006-01-01 K.OHWADA
// weblinks ver 1.0
// module depulication

//=========================================================
// WebLinks Module
// 2004/01/14 K.OHWADA
//=========================================================

$WEBLINKS_DIRNAME = basename( dirname( dirname( __FILE__ ) ) );

include_once XOOPS_ROOT_PATH.'/modules/happy_linux/include/search.php';
include_once XOOPS_ROOT_PATH.'/modules/'.$WEBLINKS_DIRNAME.'/include/functions.php';

//include_once XOOPS_ROOT_PATH.'/modules/'.$WEBLINKS_DIRNAME.'/include/weblinks_constant.php';

// --- eval begin ---
eval( '

function '.$WEBLINKS_DIRNAME.'_search( $queryarray , $andor , $limit , $offset , $uid )
{
	return weblinks_search_base( "'.$WEBLINKS_DIRNAME.'" , $queryarray , $andor , $limit , $offset , $uid ) ;
}

' );
// --- eval end ---


// === weblinks_search_base begin ===
if( !function_exists( 'weblinks_search_base' ) ) 
{

function weblinks_search_base($dirname, $queryarray, $andor, $limit, $offset, $uid)
{
	global $xoopsDB;

	$myts =& MyTextSanitizer::getInstance();

// hack for multi site
	$table_link = weblinks_multi_get_table_name($dirname, 'link');

	$where = '';
	$hightlight_key = '';

	if ( $uid != 0 ) 
	{	$where .= "uid=$uid ";	}

	$count = count($queryarray);
	if ( is_array($queryarray) && ( $count > 0 ) )
	{
		$keywords = urlencode( implode(' ', $queryarray) );
		$hightlight_key = '&amp;keywords='.$keywords;

		if ($where)  $where .= "AND ";
		$where .= "( search LIKE '%$queryarray[0]%' ";

		for ($i=1; $i<$count; $i++)
		{
			$where .= "$andor ";
			$where  .= "search LIKE '%$queryarray[$i]%' ";
		}
		$where .= ") ";
	}

	$sql = "SELECT * FROM $table_link WHERE $where ORDER BY time_update DESC";
	$result = $xoopsDB->query($sql, $limit, $offset);

	$ret = array();
	$i = 0;

	while( $row = $xoopsDB->fetchArray($result) )
	{
		$ret[$i]['link']  = 'singlelink.php?lid='.$row['lid'].$hightlight_key;
		$ret[$i]['title'] = $row['title'];
		$ret[$i]['time']  = $row['time_update'];
		$ret[$i]['uid']   = $row['uid'];
		$ret[$i]['image'] = "images/home.gif";

// show context
		$context  = $row['description'];
		$dohtml   = $row['dohtml'];
		$dosmiley = $row['dosmiley'];
		$doxcode  = $row['doxcode'];
		$doimage  = $row['doimage'];
		$dobr     = $row['dobr'];

// hack for multi language
		if ( weblinks_multi_is_japanese_site() )
		{
			if ( $row['textarea1'] )
			{
				$context  = $row['textarea1'];
				$dohtml   = $row['dohtml1'];
				$dosmiley = $row['dosmiley1'];
				$doxcode  = $row['doxcode1'];
				$doimage  = $row['doimage1'];
				$dobr     = $row['dobr1'];
			}
		}

		$context = $myts->displayTarea($context, $dohtml, $dosmiley, $doxcode, $doimage, $dobr);
		$context = preg_replace("/>/", '> ', $context);
		$context = strip_tags( $context );
		$ret[$i]['context'] = happy_linux_build_search_context($context, $queryarray);

		$i++;
	}

	return $ret;
}

// === weblinks_search_base end ===
}

?>